﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
using Benefit.Models.Week;
namespace Benefit.Service.Week
{
    /// <summary>
    /// 账户周均值
    /// </summary>
    public class ServiceAccountAvgWeek : Benefit.Interface.Week.IAccountAvgWeek
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountAvgWeek(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="weekId"></param>
        public void Delete(int weekId, int month, int year)
        {
            var query = from t in db.AccountAvgWeek where t.Week == weekId && t.Year == year select t;
            foreach (Models.Week.AccountAvgWeek c in query)
            {
                db.AccountAvgWeek.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存账号周均值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitAccountWeekAvg(int weekId, int month, int year)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            Interface.Sys.IAccount iacount = new Sys.ServiceAccount(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            foreach (Models.ServerData.T_Account account in serverAccounts)
            {
                Models.Week.AccountAvgWeek change = new Models.Week.AccountAvgWeek();

                change.AccountId = iacount.GetAccountId(account.LoginAccount);
                var query = (from t in db.AccountAvgWeek where t.Week == weekId && t.Year == year && t.AccountId == change.AccountId select t).ToList();
                var query2 = (from t in db.AccountDayChange where t.TradeHistory.WPdate == weekId && t.TradeHistory.YPdate == year && t.AccountId == change.AccountId select t).ToList();
                var query3 = (from t in db.AccountDayAnalysis where t.TradeHistory.WPdate == weekId && t.TradeHistory.YPdate == year && t.AccountId == change.AccountId select t).ToList();
                if (query.Count() > 0)
                {
                    if (query2.Count() > 0)
                    {
                        query.First().BillCount = Math.Round(query2.Average(a => a.BillCount), 2);
                        query.First().DayCount = Math.Round(query2.Average(a => a.DayCount), 2);
                        query.First().Free = Math.Round(query2.Average(a => a.Free), 2);
                        query.First().LoseCount = Convert.ToInt32(query2.Average(a => a.LoseCount));
                        query.First().Profit = Math.Round(query2.Average(a => a.Profit), 2);
                        query.First().SumLost = Math.Round(query2.Average(a => a.SumLost), 2);
                        query.First().SumWin = Math.Round(query2.Average(a => a.SumWin), 2);
                        query.First().UsedMargin = Math.Round(query2.Average(a => a.UsedMargin), 2);
                        query.First().WinCount = Convert.ToInt32(query2.Average(a => a.WinCount));
                    }
                    if (query3.Count() > 0)
                    {
                        query.First().AvgBillBenefit = Math.Round(query3.Average(a => a.AvgBillBenefit), 2);
                        query.First().AvgPostionTime = Math.Round(query3.Average(a => a.AvgPostionTime), 2);
                        query.First().MaxReturn = Math.Round(query3.Average(a => a.MaxReturn), 2);
                        query.First().OverStopLoss = Math.Round(query3.Average(a => a.OverStopLoss), 2);
                        query.First().PingJinPing = Convert.ToInt32(query3.Average(a => a.PingJinPing));
                        query.First().RaiseRate = Math.Round(query3.Average(a => a.RaiseRate), 2);
                        query.First().RiskCount = Convert.ToInt32(query3.Average(a => a.RiskCount));
                        query.First().Utilization = Math.Round(query3.Average(a => a.Utilization), 2);
                        query.First().WinRate = Math.Round(query3.Average(a => a.WinRate), 2);
                        query.First().Yield = Math.Round(query3.Average(a => a.Yield), 2);
                    }
                }
                else
                {
                    change.Month = month;
                    change.Year = year;
                    change.Week = weekId;
                    if (query2.Count() > 0)
                    {
                        change.BillCount = Math.Round(query2.Average(a => a.BillCount), 2);
                        change.DayCount = Math.Round(query2.Average(a => a.DayCount), 2);
                        change.Free = Math.Round(query2.Average(a => a.Free), 2);
                        change.LoseCount = Convert.ToInt32(query2.Average(a => a.LoseCount));
                        change.Profit = Math.Round(query2.Average(a => a.Profit), 2);
                        change.SumLost = Math.Round(query2.Average(a => a.SumLost), 2);
                        change.SumWin = Math.Round(query2.Average(a => a.SumWin), 2);
                        change.UsedMargin = Math.Round(query2.Average(a => a.UsedMargin), 2);
                        change.WinCount = Convert.ToInt32(query2.Average(a => a.WinCount));
                    }
                    if (query3.Count() > 0)
                    {
                        change.AvgBillBenefit = Math.Round(query3.Average(a => a.AvgBillBenefit), 2);
                        change.AvgPostionTime = Math.Round(query3.Average(a => a.AvgPostionTime), 2);
                        change.MaxReturn = Math.Round(query3.Average(a => a.MaxReturn), 2);
                        change.OverStopLoss = Math.Round(query3.Average(a => a.OverStopLoss), 2);
                        change.PingJinPing = Convert.ToInt32(query3.Average(a => a.PingJinPing));
                        change.RaiseRate = Math.Round(query3.Average(a => a.RaiseRate), 2);
                        change.RiskCount = Convert.ToInt32(query3.Average(a => a.RiskCount));
                        change.Utilization = Math.Round(query3.Average(a => a.Utilization), 2);
                        change.WinRate = Math.Round(query3.Average(a => a.WinRate), 2);
                        change.Yield = Math.Round(query3.Average(a => a.Yield), 2);
                    }
                    db.AccountAvgWeek.Add(change);
                }
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 获取账号周均值
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public AccountAvgWeek GetAccountAvgWeek(int accountid, int month, int week, int year)
        {
            AccountAvgWeek count = new AccountAvgWeek();
            var query = db.AccountAvgWeek.Where(a => a.AccountId == accountid).Where(a => a.Week == week).Where(a => a.Year == year);
            if (query.Count() > 0)
            {
                count = query.First();
            }
            return count;
        }


    }
}